/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lg.spring.ioc.bean;

import com.lg.spring.ioc.bean.factory.DefaultUserFactory;
import com.lg.spring.ioc.bean.factory.UserFactory;
import lombok.SneakyThrows;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import java.util.concurrent.TimeUnit;

/**
 bean初始化方法？

 注解 PostConstruct
 接口实现 InitializingBean#afterPropertiesSet()
 自定义初始化方法  @Bean(initMethod = "initUserFactory")

 @Lazy 延迟加载
 容器启动之后，bean查找的时候 才被初始化

 bean的销毁方法？
 注解 PreDestroy
 接口实现 DisposableBean#destroy()
 自定义销毁方法  @Bean(destroyMethod = "doDestroy")

 bean什么时候被垃圾回收？
 容器关闭之后，有gc出现，就会被回收
 */
@Configuration // Configuration Class
public class Initialization {

    @SneakyThrows
    public static void main(String[] args) {
        // 创建 BeanFactory 容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        // 注册 Configuration Class（配置类）
        applicationContext.register(Initialization.class);
        // 启动 Spring 应用上下文
        applicationContext.refresh();
        // 非延迟初始化在 Spring 应用上下文启动完成后，被初始化
        System.out.println("Spring 应用上下文已启动...");
        // 依赖查找 UserFactory
        UserFactory userFactory = applicationContext.getBean(UserFactory.class);
        System.out.println(userFactory);
        System.out.println("Spring 应用上下文准备关闭...");
        // 关闭 Spring 应用上下文
        applicationContext.close();
        System.out.println("Spring 应用上下文已关闭...");

        userFactory = null;
        System.gc();
        TimeUnit.SECONDS.sleep(3);
    }

    @Bean(initMethod = "initUserFactory", destroyMethod = "doDestroy")
    @Lazy
    public UserFactory userFactory() {
        return new DefaultUserFactory();
    }
}
